package com.example.myapplication.fragment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.myapplication.R;

public class FragmentActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        Button button1 = findViewById(R.id.dynamic_btn1);
        Button button2 = findViewById(R.id.dynamic_btn2);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
//
//        BlankFragment1 bf = new BlankFragment1();
//        Log.e("TAG", "onCreate:--------Callback 传参" );
//        //Callback 传参
//        bf.setFragmentCallback(new IFragmentCallback() {
//            @Override
//            public void sendMsgToActivity(String msg) {
//                Toast.makeText(FragmentActivity.this,"send msg",Toast.LENGTH_SHORT).show();
//                Log.e("TAG", "sendMsgToActivity: ----------------send"+msg );
//            }
//            @Override
//            public String getMsgFromActivity(String msg) {
//                Toast.makeText(FragmentActivity.this,"get msg",Toast.LENGTH_SHORT).show();
//                Log.e("TAG", "getMsgFromActivity: ---------------get"+msg );
//                return null;
//            }
//        });
    }

    @Override
    public void onClick(View v) {
       switch (v.getId()){
           case R.id.dynamic_btn1:
               ///Bundle 传参
               Bundle bundle = new Bundle();
               bundle.putString("message","这是一封信");
               BlankFragment1 bf = new BlankFragment1();
               bf.setArguments(bundle);
               Log.e("TAG", "onCreate:--------Callback 传参" );
               //Callback 传参
               bf.setFragmentCallback(new IFragmentCallback() {
                   @Override
                   public void sendMsgToActivity(String msg) {
                       Toast.makeText(FragmentActivity.this,"send msg",Toast.LENGTH_SHORT).show();
                       Log.e("TAG", "sendMsgToActivity: ----------------send"+msg );
                   }
                   @Override
                   public String getMsgFromActivity(String msg) {
                       Toast.makeText(FragmentActivity.this,"get msg",Toast.LENGTH_SHORT).show();
                       Log.e("TAG", "getMsgFromActivity: ---------------get"+msg );
                       return null;
                   }
               });
               replaceFragment(bf);
               break;
           case R.id.dynamic_btn2:
               replaceFragment(new ItemFragment());
       }
    }
    ///动态切换fragment
    private void replaceFragment(Fragment fragment) {
         FragmentManager fragmentManager = getSupportFragmentManager();
         FragmentTransaction fragmentTransaction =  fragmentManager.beginTransaction();
         fragmentTransaction.replace(R.id.frame_layout,fragment);
         fragmentTransaction.addToBackStack(null);
         fragmentTransaction.commit();
    }
}